<template>
    <div>
        <a-date-picker
            :default-value="defaultTime?moment(defaultTime&&defaultTime.start, dateFormat):undefined"
            format="YYYY-MM-DD HH:mm:ss"
            :disabled-date="isShowDisabled?disabledStartDate:null"
            :disabled-time="isShowDisabled?disabledStartDateTime:null"
            :show-time="{ defaultValue: moment('00:00:00', 'HH:mm:ss') }"
            @change="onChangeStart"
            :locale="locale"
            placeholder="开始时间"
        />
        -
        <a-date-picker
            :default-value="defaultTime?moment(defaultTime&&defaultTime.end, dateFormat):undefined"
            format="YYYY-MM-DD HH:mm:ss"
            :disabled-date="isShowDisabled?disabledStartDate:null"
            :show-time="{ defaultValue: moment('00:00:00', 'HH:mm:ss') }"
            @change="onChangeEnd"
            :locale="locale"
            placeholder="结束时间"
        />
        <!-- <a-date-picker
            :default-value="defaultTime?moment(defaultTime&&defaultTime.end, dateFormat):undefined"
            format="YYYY-MM-DD HH:mm:ss"
            :disabled-date="disabledEndDate"
            :disabled-time="disabledEndDateTime"
            :show-time="{ defaultValue: moment('00:00:00', 'HH:mm:ss') }"
            @change="onChangeEnd"
            :locale="locale"
            placeholder="结束时间"
        /> -->
    </div>
</template>
<script>
import moment from 'moment';
import locale from 'ant-design-vue/es/date-picker/locale/zh_CN';
export default {
    name:'rangeTime',
    props:{
        defaultTime:Object,
        isShowDisabled:{
            type:Boolean,
            default:true
        }
    },
    computed:{
        disabledTime:function(){
            return this.duringTime
        }
    },
    created(){
        this.startTime={time:this.defaultTime&&this.defaultTime.start}
        this.endTime={time:this.defaultTime&&this.defaultTime.end}
    },
    data(){
        return{
            startTime:null,
            endTime:null,
            locale,
            duringTime:null,
            dateFormat:'YYYY-MM-DD HH:mm:ss'
        }
    },
    methods:{
        moment,
        range(start, end) {
            const result = [];
            for (let i = start; i < end; i++) {
                result.push(i);
            }
            return result;
        },
        disabledStartDate(current) {
            return current && current < moment().subtract(1, "days");
        },
        disabledEndDate(current){
            return current && current < this.disabledTime
        },
        onChangeStart(data){
            this.startTime={
                'time':data,
                'day':data.date(),
                'hour':data.hour(),
                'minutes':data.minutes(),
                'second':data.second()
            }
            if(this.startTime.time.valueOf()>=this.endTime.time.valueOf()){
                this.$message.error('结束时间要大于开始时间')
                return
            }
            this.duringTime=Object.assign(data)
            this.$emit("getTimes",this.startTime.time,this.endTime.time)
        },
        disabledStartDateTime(){
            let nowDate=new Date()
            let days=nowDate.getDate()
            let hours=nowDate.getHours()
            let minutes=nowDate.getMinutes()
            if(this.startTime.day==days){
                if(this.startTime.hour==hours){
                    return {
                        disabledHours: () => this.range(0, 24).splice(0,hours),
                        disabledMinutes: () => this.range(0,minutes),
                    };
                }else{
                    return {
                        disabledHours: () => this.range(0, 24).splice(0,hours),
                    }
                }
            }
        },
        onChangeEnd(data){
            this.endTime={
                'time':data,
                'day':data.date(),
                'hour':data.hour(),
                'minutes':data.minutes()
            }
            if(this.startTime.time.valueOf()>=this.endTime.time.valueOf()){
                this.$message.error('结束时间要大于开始时间')
                return
            }
            this.$emit("getTimes",this.startTime.time,this.endTime.time)
        },
        disabledEndDateTime(){
            let day=this.startTime.day
            let hours=this.startTime.hour
            let minutes=this.startTime.minutes
            if(this.endTime.day==day){
                if(this.endTime.hour==hours){
                    if(this.endTime.minutes==this.startTime.minutes){
                        return {
                            disabledHours: () => this.range(0, 24).splice(0,hours),
                            disabledMinutes: () => this.range(0,minutes),
                            disabledSeconds: () => this.range(0,this.startTime.second),
                        };
                    }else{
                        return {
                            disabledHours: () => this.range(0, 24).splice(0,hours),
                            disabledMinutes: () => this.range(0,minutes),
                        };
                    }
                }else{
                    return {
                        disabledHours: () => this.range(0, 24).splice(0,hours),
                    }
                }
            }
        }
    }

}
</script>